package com.tql.mall.search.receiver;

import com.rabbitmq.client.Channel;
import com.tql.mall.rabbit.constant.MqConstant;
import com.tql.mall.search.service.SearchService;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: tyk
 * @Date: 2023/5/27 11:13
 * @Description:
 */
@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

// 监听上架队列
@SneakyThrows
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqConstant.QUEUE_GOODS_ON, durable = "true", autoDelete = "false"),
        exchange = @Exchange(value = MqConstant.EXCHANGE_GOODS, autoDelete = "false"),
        key = {MqConstant.ROUTING_GOODS_ON}
))
public void onSale(Long skuId, Message msg, Channel channel) {
    try {
        // skuId不为空
        if (skuId != null)
            searchService.onSale(skuId);
    } catch (Exception e) {
        // TODO 对接维护人员
    }
    channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
}

    // 监听下架队列
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConstant.QUEUE_GOODS_OFF, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConstant.EXCHANGE_GOODS, autoDelete = "false"),
            key = {MqConstant.ROUTING_GOODS_OFF}
    ))
    public void offSale(Long skuId, Message msg, Channel channel) {
        try {
            // skuId不为空
            if (skuId != null)
                searchService.offSale(skuId);
        } catch (Exception e) {
            // TODO 对接维护人员
        }
        channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
    }
}
